package com.dxd.servlet.admin;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.dxd.constant.ErrorCodeConstant;
import com.dxd.entity.PageEntity;
import com.dxd.entity.ReturnBean;
import com.dxd.entity.UserInfo;
import com.dxd.servlet.PublicServlet;

/**
 * 后端用户管理
 * 
 * @author pppcar
 *
 */
@WebServlet("/auth/admin/index")
public class IndexServlet extends PublicServlet {
	private static final long serialVersionUID = -1093647922092449357L;
	private static final Logger LOG = LoggerFactory.getLogger(IndexServlet.class);

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String type = request.getParameter("type");
		if ("userList".equals(type)) {// 查询所有的用户，包括分页，查询条件
			ReturnBean returnBean = ReturnBean.returnBeanBuild();
			try {
				String userName = request.getParameter("userName");// 用户名
				String name = request.getParameter("name");// 姓名
				PageEntity<UserInfo> data = userInfoDao.queryList(pageMap.get("page"), pageMap.get("size"), userName,
						name);
				returnBean.setData(data);
			} catch (Exception e) {
				LOG.error("查询用户信息失败，{}，{}", e.getMessage(), e);
				returnBean.setReturnCode(ErrorCodeConstant.ERROR).setReturnMsg("获取用户信息失败").setData(null);
			}
			returnJson(returnBean);
			return;
		} else if ("goUserList".equals(type)) {// 跳转到用户列表页面
			Dispatcher("/view/admin/user_list.jsp");
		} else if ("deleteUser".equals(type)) {// 删除用户
			ReturnBean returnBean = ReturnBean.returnBeanBuild();
			String userIds = request.getParameter("userIds");
			if (userIds == null || "".equals(userIds)) {
				returnBean.setReturnCode(ErrorCodeConstant.PARAM).setReturnMsg("参数为空");
				returnJson(returnBean);
				return;
			}
			userInfoDao.deleteByUserIds(userIds);
			LOG.info("删除用户成功，用户id：{}", userIds);
			returnJson(returnBean);
			return;

		} else if ("addUser".equals(type)) {
			ReturnBean returnBean = ReturnBean.returnBeanBuild();
			String userName = request.getParameter("user_name");
			String passWord = request.getParameter("pass_word");
			String name = request.getParameter("name");
			String sex = request.getParameter("sex");
			String phone = request.getParameter("phone");
			String address = request.getParameter("address");
			String isAdmin = request.getParameter("is_admin");
			String state = request.getParameter("state");
			String province = request.getParameter("province");
			String city = request.getParameter("city");
			String area = request.getParameter("area");
			String age = request.getParameter("age");
			String id = request.getParameter("id");
			String headImg = request.getParameter("headImg");
			if (headImg == null || "".equals(headImg)) {
				returnBean.setReturnCode(ErrorCodeConstant.PARAM).setReturnMsg("请上传头像");
				returnJson(returnBean);
				return;
			}
			if (userName == null || "".equals(userName)) {
				returnBean.setReturnCode(ErrorCodeConstant.PARAM).setReturnMsg("登录名不能为空");
				returnJson(returnBean);
				return;
			}
			if (passWord == null || "".equals(passWord)) {
				returnBean.setReturnCode(ErrorCodeConstant.PARAM).setReturnMsg("密码不能为空");
				returnJson(returnBean);
				return;
			}
			if (name == null || "".equals(name)) {
				returnBean.setReturnCode(ErrorCodeConstant.PARAM).setReturnMsg("姓名不能为空");
				returnJson(returnBean);
				return;
			}
			if (phone == null || "".equals(phone)) {
				returnBean.setReturnCode(ErrorCodeConstant.PARAM).setReturnMsg("电话不能为空");
				returnJson(returnBean);
				return;
			}
			if (province == null || "".equals(province) || city == null || "".equals(city) || area == null
					|| "".equals(area) || address == null || "".equals(address)) {
				returnBean.setReturnCode(ErrorCodeConstant.PARAM).setReturnMsg("地址不能为空");
				returnJson(returnBean);
				return;
			}
			// 验证用户名是否存在
			UserInfo userInfo = userInfoDao.queryByUserName(userName);
			if (userInfo != null && userInfo.getId() != null && (id == null || "".equals(id))) {
				returnBean.setReturnCode(ErrorCodeConstant.ERROR).setReturnMsg("登录户名已经存在");
				returnJson(returnBean);
				return;
			}
			userInfo = new UserInfo();
			userInfo.setUserName(userName);
			userInfo.setPassWord(passWord);
			userInfo.setAddress(address);
			userInfo.setAge(age == null ? 0 : Integer.parseInt(age));
			userInfo.setArea(area);
			userInfo.setCity(city);
			String[] headImgs = headImg.split("\\/");
			userInfo.setHeadImg(headImgs[headImgs.length - 1]);
			userInfo.setIsAdmin(isAdmin == null ? 0 : Integer.parseInt(isAdmin));
			userInfo.setName(name);
			userInfo.setPhone(phone);
			userInfo.setProvince(province);
			userInfo.setSex(sex);
			userInfo.setId(id == null ? 0 : Integer.parseInt(id));
			userInfo.setState(state == null ? 0 : Integer.parseInt(state));
			if (id == null || "".equals(id)) {
				userInfoDao.addUser(userInfo);
				LOG.info("添加用户成功，用户名：{}", userName);
			} else {
				userInfoDao.updateUser(userInfo);
				LOG.info("修改用户成功，用户名：{}", id);
			}
			returnJson(returnBean);
			return;

		} else {
			// 跳转到首页
			LOG.info("跳转到首页");
			UserInfo userInfo = getUserInfo();
			request.setAttribute("userInfo", userInfo);
			Dispatcher("/view/admin/index.jsp");
		}
	}

}